//
//  SystemResetPwdViewController.m
//  TryShoppingHD
//
//  Created by Mr.Lee on 16/7/20.
//  Copyright © 2016年 Reabam. All rights reserved.
//

#import "SystemResetPwdViewController.h"
#import "LoginViewController.h"
#import "HttpRequest.h"


@interface SystemResetPwdViewController ()<UITextFieldDelegate,UIGestureRecognizerDelegate >
@end

@implementation SystemResetPwdViewController

{
    UITextField* userNameTextField;
    UITextField* passwordTextField;
    UITextField* verificationCode;
    UIButton* ToObtain;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // 设置导航条
    [self setUpNavigationBar];
    // 设置ui
    [self setUpUI];
}


#pragma mark -- 设置导航条
- (void)setUpNavigationBar {
    //隐藏系统导航条
    self.navigationController.navigationBarHidden = YES;
    // 自定义导航条
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, StatusBarHeight, ScreenWidth, 44)];
    navView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:navView];
    
    // 设置title
    UILabel *titleLabel = [[UILabel alloc] init];
    titleLabel.frame = CGRectMake(0, 0, ScreenWidth, 44);
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont fontWithName:@"Arial" size:17];
    titleLabel.text = @"重置密码";
    [navView addSubview:titleLabel];
    
    // 返回按钮
    UIButton *backBtn = [[UIButton alloc] init];
    backBtn.frame = CGRectMake(8, 0, 44, 44);
    [backBtn setImage:[UIImage imageNamed:@"fanhui"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(BackAction:) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:backBtn];
    
    
    // 分割线
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(0, 44-0.5, ScreenWidth, 0.5)];
    lineView.backgroundColor = qianhuiColor;
    [navView addSubview:lineView];
}

#pragma mark --  点击返回
- (void)BackAction:(UIButton *)sender{
        [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -- 设置UI
- (void)setUpUI{
    
    // 密码框
    UIView* view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 105+StatusBarHeight, ScreenWidth, 250)];
    view1.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view1];
    
    UIView *line1 = [[UIView alloc]initWithFrame:CGRectMake(15, 88/2, view1.width-30, 0.5)];
    line1.backgroundColor = qianhuiColor;
    [view1 addSubview:line1];
    UIView *line3 = [[UIView alloc]initWithFrame:CGRectMake(15, 106, view1.width-30, 0.5)];
    line3.backgroundColor = qianhuiColor;
    [view1 addSubview:line3];
    
    
    // 输入密码框
    userNameTextField = [[UITextField alloc] initWithFrame:CGRectMake(15, 0, view1.width-60, 44)];
    [userNameTextField setBorderStyle:UITextBorderStyleNone]; //外框类型
    //    userNameTextField.keyboardType = UIKeyboardTypeDecimalPad;
    userNameTextField.secureTextEntry = YES; //密码
    userNameTextField.placeholder = @"请输入新密码"; //默认显示的字
    userNameTextField.font = [UIFont systemFontOfSize: 17.0];
    userNameTextField.returnKeyType = UIReturnKeyDone;
    userNameTextField.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    userNameTextField.delegate = self;
    [userNameTextField addTarget:self action:@selector(userNumberLimitTextLength:) forControlEvents:UIControlEventEditingChanged];
    [view1 addSubview:userNameTextField];
    
    
    // 再次输入密码框
    verificationCode = [[UITextField alloc] initWithFrame:CGRectMake(15, 58, 200, 44)];
    [verificationCode setBorderStyle:UITextBorderStyleNone]; //外框类型
    verificationCode.placeholder = @"请再次输入新密码"; //默认显示的字
    verificationCode.secureTextEntry = YES; //密码
    verificationCode.font = [UIFont systemFontOfSize: 17.0];
    verificationCode.autocorrectionType = UITextAutocorrectionTypeNo;
    verificationCode.autocapitalizationType = UITextAutocapitalizationTypeNone;
    verificationCode.returnKeyType = UIReturnKeyDone;
    verificationCode.clearButtonMode = UITextFieldViewModeWhileEditing; //编辑时会出现个修改X
    verificationCode.delegate = self;
    [verificationCode addTarget:self action:@selector(userNumberLimitTextLength:) forControlEvents:UIControlEventEditingChanged];
    [view1 addSubview:verificationCode];
    
    // 密码提示
    UILabel* label=[[UILabel alloc]initWithFrame:CGRectMake(0,CGRectGetMaxY(line3.frame) +10, view1.width, 35)];
    label.text=@"密码由6-20位英文字母或数字组成";
    label.font= [UIFont systemFontOfSize:13];
    label.textAlignment=NSTextAlignmentCenter;
    [label setTextColor:[UIColor colorWithRed:153.0/255.0 green:153.0/255.0 blue:153.0/255.0 alpha:1.0]];
    [view1 addSubview:label];
    
    
    // 确定
    UIButton* login = [[UIButton alloc] initWithFrame:CGRectMake(15, 303+StatusBarHeight, ScreenWidth-30, 48)];
    [login setTitle:@"确定" forState:UIControlStateNormal];
    [login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    login.backgroundColor=ZhuTibtn;
    [login.layer setCornerRadius:4.0]; //设置矩圆角半径
    [login.layer setBorderWidth:0.5];   //边框宽度
    CGColorSpaceRef tureBTnSpace1 = CGColorSpaceCreateDeviceRGB();
    CGColorRef tureBTncolorref1 = CGColorCreate(tureBTnSpace1,(CGFloat[]){204.0/255.0,204.0/255.0, 204.0/255.0, 1.0 });
    [login.layer setBorderColor:tureBTncolorref1];//边框颜色
    [login addTarget:self action:@selector(regist:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:login];
}


// 点击确定
-(void)regist:(UIButton*)sender{
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSString* Token = [user objectForKey:@"Token"];
    NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
    params[@"Token"] = Token;
    
    
    params[@"passwd"] = [SecutiryTools md5:userNameTextField.text];//userNameTextField.text.md5String;
    params[@"rpasswd"] = [SecutiryTools md5:verificationCode.text];//verificationCode.text.md5String;
    params[@"ucode"] = self.ucode;
    params[@"vcode"] = self.vcode;
    
//    DLog(@"====%@",params);
    [HttpRequest requestData1:@"Common/ResetPwd" params:params success:^(BOOL isSuccess, SeverData *severDataDict) {
        if (isSuccess) {
//            DLog(@"====%@",severDataDict.dataDict);
            
            if ([[severDataDict.dataDict objectForKey:@"ResultInt"] integerValue]==0) {
                [MBProgressHUD hideProgressHUDInternal:self.view];
                
                NSUserDefaults *user=[NSUserDefaults standardUserDefaults];
                [user setObject:self.ucode forKey:@"userNameTextField"];
                [user setObject:userNameTextField.text forKey:@"passwordTextField"];
                [user synchronize];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    for (UIViewController *controller in self.navigationController.viewControllers) {
                        if ([controller isKindOfClass:[LoginViewController class]]) {
                            LoginViewController *login =(LoginViewController *)controller;
                            [self.navigationController popToViewController:login animated:YES];
                        }
                    }
                });
                
            }else{
                
                
                [MBProgressHUD hideProgressHUDInternal:self.view];
                NSString *requestResultStr=[severDataDict.dataDict objectForKey:@"ResultString"];
                [MBProgressHUD showTextHudSub:requestResultStr view:self.view];
            }
            
        } else {
            [MBProgressHUD hideProgressHUDInternal:self.view];
            [MBProgressHUD showTextHudSub:@"连接失败" view:self.view];
        }
    }];
    
}

#pragma mark -- texte
- (void)userNumberLimitTextLength:(UITextField*)textField
{
    if ([textField.text length] > 11)
    {
        textField.text = [textField.text substringToIndex:11];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}


@end
